import { Editor, Transforms, Element, Location } from 'slate';

export function setBlockNode(
  editor: Editor,
  config: object,
  options: {
    at?: Location;
  } = {}
) {
  const { at } = options;
  Transforms.setNodes(editor, config, {
    match: (n) => !Editor.isEditor(n) && Element.isElement(n),
    at,
  });
}

export function setWrapNode(
  editor: Editor,
  element: object,
  options: {
    at?: Location;
  } = {}
) {
  const { at } = options;
  Transforms.wrapNodes(
    editor,
    {
      ...element,
      children: [],
    },
    {
      at,
      match: (n) => !Editor.isEditor(n) && Element.isElement(n),
    }
  );
}
